package com.kwan.widget.float_view;

import android.app.Activity;
import android.app.Application;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.BounceInterpolator;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.kwan.widget.float_view.floatwindow.FloatActivity;
import com.kwan.widget.float_view.floatwindow.FloatApp;
import com.kwan.widget.float_view.floatwindow.FloatLifecycle;
import com.kwan.widget.float_view.floatwindow.FloatWindow;
import com.kwan.widget.float_view.floatwindow.LifecycleListener;
import com.kwan.widget.float_view.floatwindow.MoveType;
import com.kwan.widget.float_view.floatwindow.Screen;
import com.kwan.widget.float_view.floatwindow.ViewStateListener;

import timber.log.Timber;

import static com.kwan.widget.float_view.floatwindow.FloatWindow.mDefaultTag;


public class FloatViewManager implements View.OnClickListener {

	public static boolean hasPermission;
	public static boolean isRequestPermission;
	public FloatLifecycle mFloatLifecycle;

	public void init(final Activity activity, Application application) {

		//请求权限
		FloatActivity.request(application, new PermissionListener() {
			@Override
			public void onSuccess() {
				Timber.e("权限请求成功 hasPermission:" + hasPermission);
				if (!hasPermission) {
					hasPermission = true;
					isRequestPermission = true;
					getFloatWindowBuilder(application).setView(createView(application))
							.setTag(mDefaultTag)
							.build();
				}
			}

			@Override
			public void onFail() {
				Timber.e(activity + "权限请求失败 创建应用内 悬浮窗 .....");
				hasPermission = false;
				isRequestPermission = true;
				getFloatWindowBuilder(application).setView(createView(activity))
						.setTag(activity.getClass().getName())
						.build();
			}
		});


		mFloatLifecycle = new FloatLifecycle(application,
				true,
				mActivities,
				new LifecycleListener() {
					@Override
					public void onCreate(Activity activity) {
						Timber.e(activity + " onCreate");

						if (!hasPermission && isRequestPermission) {
							Timber.e(activity + " onCreate 创建应用内 悬浮窗");
							getFloatWindowBuilder(application)
									.setView(createView(activity))
									.setX(FloatApp.mX)
									.setY(FloatApp.mY)
									.setTag(activity.getClass().getName())
									.build();
						}
					}

					@Override
					public void onDestroyed(Activity activity) {

						if (!hasPermission && isRequestPermission) {
							Timber.d(activity + " onDestroyed 销毁应用内 悬浮窗");
							FloatWindow.destroy(activity.getClass().getName());
						}
					}

					@Override
					public void onResumedShow(Activity activity) {
						Timber.d(activity + " onResumedShow");
						show(activity, true);
					}

					@Override
					public void onResumedHide(Activity activity) {
						Timber.d(activity + " onResumedHide");
						hide(activity);
					}

					@Override
					public void onBackToDesktop(Activity activity) {

//						if (!mB.mDesktopShow) {
//							hide(activity);
//						}
//
//						if (mB.mViewStateListener != null) {
//							mB.mViewStateListener.onBackToDesktop();
//						}
					}
				});


	}

	/**
	 * 应用内悬浮窗的View
	 *
	 * @param activity
	 * @return
	 */

	public View createView(Activity activity) {

		View flaView = LayoutInflater.from(activity).inflate(mFloatViewResId, null);
		FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		flaView.setLayoutParams(params);
		flaView.setOnClickListener(FloatViewManager.this);

		Timber.d("createView # " + flaView + " " + activity);

		return flaView;
	}

	/**
	 * 全局悬浮窗的View
	 *
	 * @return
	 */

	public static View mPhoneView;

	public View createView(Application application) {

		if (mPhoneView == null) {

			mPhoneView = LayoutInflater.from(application).inflate(mFloatViewResId, null);
			FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
					RelativeLayout.LayoutParams.WRAP_CONTENT,
					RelativeLayout.LayoutParams.WRAP_CONTENT);
			mPhoneView.setLayoutParams(params);
			mPhoneView.setOnClickListener(FloatViewManager.this);
		}

		return mPhoneView;
	}

	private final Class<? extends Activity>[] mActivities;
	private final int mFloatViewResId;

	FloatWindow.Builder getFloatWindowBuilder(Application application) {
		return FloatWindow
				.with(application.getApplicationContext())
				.setWidth(Screen.width, 0.2f) //设置悬浮控件宽高
				.setHeight(Screen.height, 0.2f)
				.setX(Screen.width, 0.8f)
				.setY(Screen.height, 0.8f)
				.setMoveType(MoveType.slide, 0, 0)
				.setMoveStyle(500, new BounceInterpolator())
				.setViewStateListener(mViewStateListener)
				.setDesktopShow(true);
	}

	public FloatViewManager(int floatViewResId, Class<? extends Activity>... activities) {
		mFloatViewResId = floatViewResId;
		mActivities = activities;
	}


	public void show(Activity activity, boolean isResumed) {

		Timber.e("show # hasPermission:" + hasPermission + " isRequestPermission:" + isRequestPermission);

		if (!hasPermission && isRequestPermission) {
			Timber.d(activity + " show 应用内 isResumed:"+isResumed);

			if (isResumed) {
				FloatWindow.get(activity.getClass().getName()).updateShow(activity);
			} else {
				FloatWindow.get(activity.getClass().getName()).show(activity);
			}

		} else if (hasPermission) {
			Timber.d(activity + " onShow 全局");

			FloatWindow.get().show(activity);
		} else {
			Timber.e(activity + "没有权限也没有申请过权限 不处理");
		}

	}


	public void show(Activity activity) {

		show(activity, false);

	}

	public void hide(Activity activity) {

		if (!hasPermission && isRequestPermission) {
			Timber.d(activity + " onHide 应用内");
			FloatWindow.get(activity.getClass().getName()).hide(activity);
		} else if (hasPermission) {
			Timber.d(activity + " onHide 全局");
			FloatWindow.get().hide(activity);
		} else {
			Timber.e(activity + "没有权限也没有申请过权限 不处理");
		}

	}


	private ViewStateListener mViewStateListener = new ViewStateListener() {
		@Override
		public void onPositionUpdate(int x, int y) {
			Timber.d("onPositionUpdate: x=" + x + " y=" + y);
		}

		@Override
		public void onShow() {
			Timber.d("onShow");
		}

		@Override
		public void onHide() {
			Timber.d("onHide");
		}

		@Override
		public void onDismiss() {
			Timber.d("onDismiss");
		}

		@Override
		public void onMoveAnimStart() {
			Timber.d("onMoveAnimStart");
		}

		@Override
		public void onMoveAnimEnd() {
			Timber.d("onMoveAnimEnd");
		}

		@Override
		public void onBackToDesktop() {
			Timber.d("onBackToDesktop");
		}
	};


	@Override
	public void onClick(View v) {

	}


}
